<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>
<form class="layui-form">
    <div class="layui-inline">
        <label class="layui-form-label">选择角色：</label>
        <div class="layui-input-inline">
            <select lay-filter="selectRole" name="role" lay-verify="role" lay-search="">
                <option value="">---请选择角色---</option>
                <option th:selected="${role.id==id}" th:each="role : ${roles}" th:value="${role.id}" th:text="${role.nameZh}"></option>
            </select>
        </div>
    </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">权限：</label>
        <div class="layui-input-block">
            <input th:each="menu : ${level1Menus}" th:checked="${menuIdList.contains(menu.id)}"  type="checkbox" name="level1" th:value="${menu.id}" th:title="${menu.menuName}">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" id="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });



        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            role: function (value) {
                if (value==='') {
                    return '请选择角色再提交';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听选中下拉框
        form.on('select(selectRole)', function (data) {
            //选中后发送角色id查询拥有权限的menus
            location='/user/menu/menuform2.html?id='+data.elem.value;
            return false;
        });

        form.on('submit(demo1)', function (data) {
            var arr = new Array();
            $("input:checkbox[name='level1']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            data.field.level1 = arr.join(",");
            //做权限分配
            $.ajax({
                url: '/allocation'
                , method: 'post'
                , data: data.field
                , success: function (res) {
                    layer.msg("权限修改成功");
                    setTimeout(function () {
                        window.parent.location.reload();//修改成功后刷新父界面
                    }, 1000);
                }
            });
            return false;
        });
    })
    ;
</script>
</html>